/* ---
 * Dojo Requires
 * ---
 */

// Layout
dojo.require('dijit.layout.BorderContainer');
dojo.require('dijit.layout.ContentPane');
dojo.require('dijit.layout.TabContainer');

// Menu
dojo.require('dijit.MenuBar');
dojo.require('dijit.Menu');
dojo.require('dijit.MenuItem');
dojo.require('dijit.PopupMenuBarItem');

// Widgets
dojo.require('dijit.Dialog');


/* ---
 * Internal configuration
 * ---
 */

var UI = {};

// Identification
UI.name = 'admin2';
UI.version = '0.1';
UI.beta = true;

// Configuration
UI.debug = djConfig.isDebug;

/**
 * Log an object to console
 *
 * @param obj Object that will be output to console.
 */
UI.log = function(obj) {
	if (UI.debug && console) {
		console.log(obj);

	}
};

/**
 * Output formatted informational messages
 *
 * @param msg Message to output to console.
 */
UI.info = function(msg) {
	if (UI.debug && console) {
		console.log('INFO: ' + msg);
	}
};

/* ---
 * Build panels and other UI elements,
 * and initialize them.
 * ---
 */

dojo.ready(function(appplication) {
	/*
     *  PANELS
     */

	UI.info('Creating panels');

	// Master container
	UI.bc = new dijit.layout.BorderContainer({
		id: 'mainContainer'
	});

	// Top panel
	UI.topPanel = new dijit.layout.ContentPane({
		region: 'top',
		id: 'topPanel',
		content: 'Top panel'
	});

	// Bottom panel
	UI.bottomPanel = new dijit.layout.ContentPane({
		region: 'bottom',
		id: 'bottomPanel',
		content: 'Bottom panel'
	});

	// Application list to the left
	UI.appList = new dijit.layout.ContentPane({
		title: 'Applications',
		region: 'left',
		id: 'leftPanel',
		content: 'Left panel',
		splitter: true
	});

	// Main panel that will contain the contents
	UI.mainPanel = new dijit.layout.BorderContainer({
		region: 'center',
		id: 'mainPanel'
	});

	// Main content are for main panel
	UI.mainPanelContents = new dijit.layout.TabContainer({
		region: 'center',
		id: 'mainPanelContents'
	});

	// Attach main panel subpanels
	UI.mainPanel.addChild(UI.mainPanelContents);

	UI.info('Attaching main panels to UI');
	// Attach panels
	UI.bc.addChild(UI.topPanel);
	UI.bc.addChild(UI.mainPanel);
	UI.bc.addChild(UI.bottomPanel);
	UI.bc.addChild(UI.appList);

	/*
     * MENU
     */

    UI.menus = {};

	UI.info('Building menu');
	// Menu bar
	UI.mainPanelMenu = new dijit.MenuBar({
		region: 'top'
	});

	// Help menu
	UI.menus.help = new dijit.Menu({});
	UI.menus.help.addChild(new dijit.MenuItem({
		label: 'About'
	}));
	UI.menus.help.addChild(new dijit.MenuItem({
		label: 'web2py Book'
	}));
	UI.menus.help.name = 'Help';

	UI.menuRefresh = function() {
        UI.info('Refreshing main menu');
		for (menu in UI.menus) {
			if (dojo.isString(UI.menus[menu].name)) {
                var m = UI.menus[menu];
				UI.mainPanelMenu.addChild(new dijit.PopupMenuBarItem({
					label: m.name,
					popup: m
				}));
			}
		}
	};

    UI.menuRefresh();
	UI.mainPanel.addChild(UI.mainPanelMenu);

	/*
     * INIT
     */

	// Add the base container to BODY
	document.body.appendChild(UI.bc.domNode);
	UI.bc.startup();
	UI.info('UI ready');

	// Boot the application
	UI.info('Booting application');
	application(UI);
});

